Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inlay hints for IndexSignature #56580

Merged
merged 2 commits into from
Nov 29, 2023
Merged

Conversation

PranavSenthilnathan
Copy link
Member

Fixes #56559

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 28, 2023
@PranavSenthilnathan PranavSenthilnathan merged commit 697025d into main Nov 29, 2023
19 checks passed
@PranavSenthilnathan PranavSenthilnathan deleted the pranas/more-inlay-hints branch November 29, 2023 18:51
@@ -750,6 +751,17 @@ export function provideInlayHints(context: InlayHintsContext): InlayHint[] {
visitForDisplayParts(node.type);
}
break;
case SyntaxKind.IndexSignature:
Debug.assertNode(node, isIndexSignatureDeclaration);
Debug.assertEqual(node.parameters.length, 1);
Copy link
Member

@DanielRosenwasser DanielRosenwasser Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if an assertion is appropriate - what happens if you have something like this?

const x: { []: string }
const y = x;

Copy link
Member

@DanielRosenwasser DanielRosenwasser Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, assertions shouldn't assume well-formed code, they should assert invariants of how the compiler constructs the tree. You'd probably be better off with just visitParametersAndTypeParameters or node.parameters.forEach(visitForDisplayParts).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get An index signature must have exactly one parameter.ts(1096) for that code. Also, the parser doesn't create an index signature node for that (the type literal node just has no member children nodes) so this switch case isn't hit,

That being said, I also wasn't so enthusiastic about using the assert but wasn't sure about alternatives. Your suggestion is better though since this is a user error and we already report diagnostics to the user elsewhere.

I think this should be represented in the types though so we can enforce the invariant. Can we just add something like:

export interface IndexSignatureDeclaration extends SignatureDeclarationBase, ...
{
    ...
    readonly parameters: [ParameterDeclaration] & NodeArray<ParameterDeclaration>;
}

It's probably not that easy since this is a public API and would be breaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error in 'visitForDisplayParts' during 'provideInlayHints'
4 participants